#ifndef cblp__XmlGenerator__h
#define cblp__XmlGenerator__h


#include <QtXml>


namespace cblp
{

    class XmlGenerator:
        private QDomDocument
    {

    public:

        typedef QDomDocument super;

        XmlGenerator(const QDomDocument & other):
            super(other),
            root(documentElement())
        {
        }

        /// Creates a document with an element named with the given tag.
        XmlGenerator(const QString & name):
            root(createElement(name))
        {
            super::appendChild(root);
        }

        /// Appends an attribute to the root element.
        template<typename T>
        XmlGenerator & operator() (const QString & name, const T & value)
        {
            root.setAttribute(name, value);
            return *this;
        }

        /// Fills the body of the root element with the given text.
        XmlGenerator & operator[] (const QString & text)
        {
            root.appendChild(createTextNode(text));
            return *this;
        }

        /// Appends a subelement to the root element.
        XmlGenerator & appendChild(const QDomDocument & doc)
        {
            root.appendChild(doc.documentElement());
            return *this;
        }

        /// Appends a subelement to the root element.
        XmlGenerator & appendChild(const XmlGenerator & doc)
        {
            root.appendChild(doc.root);
            return *this;
        }

        template <typename T>
        XmlGenerator & operator[] (const T & subDoc)
        {
            return appendChild(subDoc);
        }

        /// We give access to some QDomDocument's methods.
        super::toDocument;
        super::toString;

    private:

        QDomElement root;

    }; // class XmlGenerator

} // namespace cblp


#endif // cblp__XmlGenerator__h
